# -*- coding:UTF-8 -*-
from bs4 import BeautifulSoup
import requests, sys

hostname = 'http://www.jinyingjie.com'

url = 'http://www.jinyingjie.com/login.html' # 登陆页

target = 'http://www.jinyingjie.com/kouqiang/tiku.html' # 登陆之后的首页

chapter = 'http://www.jinyingjie.com/NewTiku/son_section.html' # 章列表

headers = {'Cookie':'aliyungf_tc=AQAAAD8DWnrJoAEAiW/Ab0VDYviY6wnH; PHPSESSID=qves9v6ehv610l0di1fto44192; uid=1109251; fenxiao=1109251; UM_distinctid=1671784827230-0b7029cb52be68-1e3d6655-13c680-16717848273b70; CNZZDATA1430937=cnzz_eid%3D1081580908-1542286403-http%253A%252F%252Fwww.jinyingjie.com%252F%26ntime%3D1542286403; MEIQIA_VISIT_ID=1D1e23AYlggvqDAxC4vsFdXm1TX; MEIQIA_EXTRA_TRACK_ID=1D1e1xx7aWPtdPL0Li80CGsSgcA'}
# req = requests.get(target,headers = headers, verify=False)
# html = req.text
# print(html)

# data = {'id':115, 'pro':45, 'subjectid':17}
# chapterlist = requests.post(chapter,data = data, headers = headers, verify = False)
# content = chapterlist.text
# print(content)



# 先找到章节练习，把科目的名字取出来
# def chapter():
req = requests.get(target,headers = headers, verify=False)
html = req.text
div_tu = BeautifulSoup(html)
div_1 = div_tu.find_all('div', id= 'tk')
div_s = BeautifulSoup(str(div_1[0]))
div_2 = div_s.find_all('div',class_ = 'tk_zhang')
length = len(div_2)
kemu_array = [] # 科目名称
kemu_id_array = [] # 科目对应的id
for i in div_2:
	id = i.attrs['id'].replace('zhang', '')
	write_flag = True
	with open('题库.txt', 'a', encoding='utf-8') as f:
		f.write('【科目名称】'+ i.string.strip() + '\n\n\n\n\n\n')

		# 获取科目下的章节
		data = {'id':id, 'pro':45, 'subjectid':17}
		chapterlist = requests.post(chapter,data = data, headers = headers, verify = False)
		content = chapterlist.text
		ch_1 = BeautifulSoup(content)
		ch_s = ch_1.find_all('a')
		for x in ch_s:
			f.write('\n\n'+'【章名称】' + x.string + '\n\n')
			u = x.attrs['href']
			#获取章节里面的题
			tilist = requests.get(hostname + u, headers = headers, verify = False)
			ti_1 = tilist.text
			ti_s = BeautifulSoup(ti_1)
			ti_2 = ti_s.find_all('div', class_ = 'answerCenter')


			# 下载在txt文档中
			# write_flag = True
			# with open('题库.txt', 'a', encoding='utf-8') as f:
			# 	f.write(x.string + '\n')
			for q in ti_2:
				num = BeautifulSoup(q.text)
				# exit()
				f.writelines(num)

				# f.writelines(text)
				f.write('\n\n')
print('下载完成')
















